package com.hdoit.apollo.web.cargo.apis;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.druid.util.StringUtils;
import com.hdoit.apollo.logical.cargo.model.Cargo;
import com.hdoit.apollo.logical.cargo.model.Collocation;
import com.hdoit.apollo.logical.cargo.model.StandardPropertyUser;
import com.hdoit.apollo.logical.cargo.model.StandardUser;
import com.hdoit.apollo.logical.cargo.service.ICargoService;
import com.hdoit.apollo.logical.cargo.service.ICollocationService;
import com.hdoit.apollo.logical.common.enums.cargo.CargoState;
import com.hdoit.apollo.logical.stock.model.Stock;
import com.hdoit.apollo.logical.stock.service.IStockCollocationService;
import com.hdoit.apollo.logical.stock.service.IStockService;
import com.hdoit.apollo.logical.viewmodel.cargo.CargoSearch;
import com.tennetcn.authority.component.webapi.BaseApi;
import com.tennetcn.authority.component.webapi.ResponseModel;
import com.tennetcn.authority.component.webapi.ResponseStatus;
import com.tennetcn.core.common.enums.pub.YesOrNoInteger;
import com.tennetcn.core.common.utils.DateUtils;
import com.tennetcn.core.common.utils.JsonUtils;
import com.tennetcn.data.enums.ModelStatus;
import com.tennetcn.data.message.PagerModel;


@Controller
@RequestMapping(value = "/api/v1/cargo/",method = { RequestMethod.GET,RequestMethod.POST},produces = "application/json;charset=UTF-8")
@CrossOrigin(origins = "*", maxAge = 3600)
@Api(value="/api/v1/cargo")
public class CargoApi extends BaseApi{
	
	@Autowired
	private ICargoService cargoService;

	@Autowired
	private ICollocationService collocationService;
	
	@Autowired
	private IStockService stockService;
	
	@Autowired
	private IStockCollocationService stockCollocationService;
	
	private final String getCargoInfoListApi="getCargoInfoList";
	private final String loadCargo="loadCargo";
	
	
	private final String saveCargoApi="saveCargo";
	private final String getCargoByCargoNo="getCargoByCargoNo";
	private final String checkCargoNo="checkCargoNo";
	
	private final String changeCargoStateApi="changeCargoState";
	
	private final String getCollocationByCargoIdApi="getCollocationByCargoId";
	private final String deleteCargo="deleteCargo";
	
	@RequestMapping(value = getCollocationByCargoIdApi)
    @ApiOperation(notes = getCollocationByCargoIdApi, httpMethod = "GET", value = "价格配置信息")
    public @ResponseBody String getCollocationByCargoId(String cargoId){
    	List<Collocation> collocations = collocationService.queryListByCargoId(cargoId);
    	
    	ResponseModel rm=new ResponseModel();
    	rm.put("collocations",collocations);
    	return rm.toJson();
    }
	
	
	@RequestMapping(value = changeCargoStateApi)
    @ApiOperation(notes = changeCargoStateApi, httpMethod = "POST", value = "上架下架变更")
    public @ResponseBody String changeCargoState(Cargo cargo){
    	Cargo cargoed=cargoService.queryModel(cargo.getCargoId());
    	
    	
    	if (("offSheleves").equals(cargoed.getCargoState())) {
    		cargoed.setCargoState(CargoState.onSheleves);
    		
		}else {
			cargoed.setCargoState(CargoState.offSheleves);
			
		}
		
    	cargoed.setModifyDate(DateUtils.getCurrent());
		
    	cargoed.setModifyUserId(getLoginId());
    	cargoed.setModifyUserName(getLoginName());
    	cargoed.setModelStatus(ModelStatus.update);
    	   	
     	ResponseModel rm=new ResponseModel();
     	if(!cargoService.applyChange(cargoed)){
     		rm.setStatus(ResponseStatus.dataError);
     	}
     	return rm.toJson();
    }
	 
    @RequestMapping(value = getCargoInfoListApi)
    @ApiOperation(notes = getCargoInfoListApi, httpMethod = "POST", value = "获取货物信息列表")
    public @ResponseBody String getCargoInfoList(CargoSearch search,int dataPosition,int pageSize){
    	ResponseModel rm=new ResponseModel();
    	PagerModel pagerModel=null;
    	if(pageSize!=0){
    		int pageindex = (dataPosition%pageSize==0?dataPosition/pageSize:dataPosition/pageSize+1)+1;
    		pagerModel = new PagerModel(pageSize, pageindex);
    	}else{
    		pagerModel=new PagerModel();
    	}
    	int cargosNum=cargoService.queryCountMP(search);
    	List<Cargo> cargos = cargoService.queryListMP(search, pagerModel);
    	System.err.println(cargos.size());
    	
    	rm.put("cargosNum",cargosNum);
    	rm.put("cargos",cargos);
    	return rm.toJson();
    }
    
    @RequestMapping(value = loadCargo)
    @ApiOperation(notes = loadCargo, httpMethod = "POST", value = "获取货物信息详情")
    public @ResponseBody String loadCargo(String cargoId){
    	ResponseModel rm=new ResponseModel();

    	Cargo cargo = cargoService.queryCargoById(cargoId);

    	rm.put("cargo",cargo);
    	return rm.toJson();
    }
    
    @RequestMapping(value = deleteCargo)
    @ApiOperation(notes = deleteCargo, httpMethod = "POST", value = "删除货物信息")
    public @ResponseBody String deleteCargo(String cargoId){
    	ResponseModel rm=new ResponseModel();
    	collocationService.deleteByCargoId(cargoId);
    	Stock stock=stockService.queryModelByKey(cargoId);
    	stockCollocationService.deleteByStockId(stock.getStockId());
    	stockService.deleteByCargoId(cargoId);
    	if(!cargoService.deleteCargoById(cargoId)) {
    		rm.setStatus(ResponseStatus.dataError);
    	}
    	return rm.toJson();
    }
    
    @RequestMapping(value = saveCargoApi)
    @ApiOperation(notes = saveCargoApi, httpMethod = "POST", value = "保存货品信息")
    public @ResponseBody String saveCargo(Cargo cargo,String collocationsJson,String standardUserJson,String standardPropertyUsersJson){
    	Cargo cargoed=cargoService.queryModel(cargo.getCargoId());
    	 
    	if (cargoed==null) {
    		if(StringUtils.isEmpty(cargo.getCategoryName())) {
    			cargo.setCategoryName("未分类");
    		}
    		cargo.setCreateDate(DateUtils.getCurrent());
    		cargo.setCreateUserId(getLoginId());
    		cargo.setCreateUserName(getLoginName());
    		cargo.setDeleteMark(YesOrNoInteger.no);
    		cargo.setModelStatus(ModelStatus.add);
    		if (StringUtils.isEmpty(cargo.getCargoType())) {
    			cargo.setCargoType("库存商品");
			}
    		cargo.setCargoState(CargoState.onSheleves);
		}else {
			cargo.setModifyDate(DateUtils.getCurrent());
			cargo.setModifyUserId(getLoginId());
			cargo.setModifyUserName(getLoginName());
			cargo.setModelStatus(ModelStatus.update);
			cargo.setDeleteMark(cargoed.getDeleteMark());
		}
    	System.err.println(getApiLoginModel().getArgument("shopId"));
    	@SuppressWarnings("unchecked")
    	List<StandardUser> standardUsers=(List<StandardUser>) JsonUtils.readValues(standardUserJson, List.class, StandardUser.class);

    	@SuppressWarnings("unchecked")
    	List<StandardPropertyUser> standardPropertyUsers=(List<StandardPropertyUser>) JsonUtils.readValues(standardPropertyUsersJson, List.class, StandardPropertyUser.class);
    	
    	
    	@SuppressWarnings("unchecked")
    	List<Collocation> collocations= (List<Collocation>) JsonUtils.readValues(collocationsJson, List.class, Collocation.class);
    	
    	
     	ResponseModel rm=new ResponseModel();
     	if(!cargoService.applyChange(cargo,standardUsers,standardPropertyUsers,collocations)){
     		rm.setStatus(ResponseStatus.dataError);
     	}
     	
     
     	return rm.toJson();
    }
    
    @RequestMapping(value = getCargoByCargoNo)
    @ApiOperation(notes = getCargoByCargoNo, httpMethod = "GET", value = "通过条形码获取货物信息")
    public @ResponseBody String getCargoByCargoNo(String cargoNo,String shopId){
    	//这里的cargoNo是条形码
    	Cargo cargo=cargoService.getCargoByShapeCode(cargoNo,shopId);
    	ResponseModel rm=new ResponseModel();
    	rm.put("cargo",cargo);
    	return rm.toJson();
    }
    
    @RequestMapping(value = checkCargoNo)
    @ApiOperation(notes = checkCargoNo, httpMethod = "GET", value = "检测cargoNo是否唯一")
    public @ResponseBody String checkCargoNo(String cargoNo,String shopId){
    	ResponseModel rm=new ResponseModel();
    	 if(cargoService.checkCargoNo(null, cargoNo,shopId)){
    		rm.setStatus(ResponseStatus.dataError);
 			rm.setMessage("已经存在相同货物编号了");
 		}
    	return rm.toJson();
    }
}

